"""
Phase 2: Baseline Regressions — Sectoral Savings Decomposition
================================================================
Tests:
  (a) Z → gross national savings (replicates baseline)
  (b) Z → govt saving vs private saving (decomposition)
  (c) Z → private consumption (inverse of private saving)
  (d) Z → savings-investment gap
  (e) OECD vs non-OECD; income terciles
  (f) Interactions and mediation

Output: output/tables/baseline_savings.md, decomposition.md
"""

import sys
from pathlib import Path
import numpy as np
import pandas as pd

PROJECT_DIR = Path("/mnt/c/demographics_capital_flows/sectoral_savings")
ROOT_DIR = PROJECT_DIR.parent
sys.path.insert(0, str(ROOT_DIR / "multilateral" / "src"))
from model import PanelGLS

PROCESSED_DIR = PROJECT_DIR / "data" / "processed"
TABLES_DIR = PROJECT_DIR / "output" / "tables"

OECD_38 = [
    "AUS", "AUT", "BEL", "CAN", "CHL", "COL", "CRI", "CZE", "DNK", "EST",
    "FIN", "FRA", "DEU", "GRC", "HUN", "ISL", "IRL", "ISR", "ITA", "JPN",
    "KOR", "LVA", "LTU", "LUX", "MEX", "NLD", "NZL", "NOR", "POL", "PRT",
    "SVK", "SVN", "ESP", "SWE", "CHE", "TUR", "GBR", "USA",
]


def stars(p):
    if p < 0.01: return '***'
    if p < 0.05: return '**'
    if p < 0.10: return '*'
    return ''


def run_model(df, dep_var, regressors, label):
    regressors = [r for r in regressors if r in df.columns]
    if dep_var not in df.columns:
        print(f"  [{label}] {dep_var} missing — skipping")
        return None
    sub = df.dropna(subset=[dep_var] + regressors).copy()
    if len(sub) < 50:
        print(f"  [{label}] Insufficient obs ({len(sub)}) — skipping")
        return None
    gls = PanelGLS()
    gls.fit(sub[dep_var].values, sub[regressors].values,
            sub['iso3'].values, sub['year'].values)
    print(f"\n  [{label}]  N={gls.n_obs}, countries={gls.n_countries}, R²={gls.r_squared:.4f}")
    results = {
        'label': label, 'dep_var': dep_var,
        'n_obs': gls.n_obs, 'n_countries': gls.n_countries,
        'r_squared': gls.r_squared,
    }
    for i, name in enumerate(regressors):
        results[f'coef_{name}'] = gls.beta[i]
        results[f'se_{name}'] = gls.se[i]
        results[f'p_{name}'] = gls.pvalues[i]
        sig = stars(gls.pvalues[i])
        print(f"    {name:<30} {gls.beta[i]:>10.4f} ({gls.se[i]:.4f}) {sig}")
    return results


def build_table(results, key_vars, notes, filename, title):
    if not results:
        return
    md = [f"# {title}\n"]
    md.append("| Model | Dep Var | N | Countries | R² |")
    md.append("|---|---|---|---|---|")
    for r in results:
        md.append(f"| {r['label']} | {r['dep_var']} | {r['n_obs']:,} "
                  f"| {r['n_countries']} | {r['r_squared']:.3f} |")
    md.append("\n## Key Coefficients\n")
    md.append("| Model | Variable | Coef | SE | p-value | Sig |")
    md.append("|---|---|---|---|---|---|")
    for r in results:
        for var in key_vars:
            ckey = f'coef_{var}'
            if ckey in r:
                p = r[f'p_{var}']
                md.append(f"| {r['label']} | {var} | {r[ckey]:.4f} "
                          f"| {r[f'se_{var}']:.4f} | {p:.4f} | {stars(p)} |")
    md.append(f"\n*{notes}*")
    out = TABLES_DIR / filename
    out.write_text('\n'.join(md))
    print(f"\n  Saved: {out}")


def main():
    print("=" * 70)
    print("PHASE 2: Baseline — Sectoral Savings Decomposition")
    print("=" * 70)

    df = pd.read_csv(PROCESSED_DIR / "sectoral_panel.csv")
    print(f"Panel: {df['iso3'].nunique()} countries, {len(df):,} obs")

    demo_vars = ['Z_1', 'Z_2', 'Z_3']
    age_vars = ['old_dep', 'youth_dep']
    controls = ['rgdp_growth', 'kaopen', 'nfa_gdp_lag']
    controls = [c for c in controls if c in df.columns]
    controls_inc = controls + (['log_gdp_pc'] if 'log_gdp_pc' in df.columns else [])

    oecd = df[df['oecd'] == 1].copy()
    non_oecd = df[df['oecd'] == 0].copy()

    # ═══════════════════════════════════════════════════════════════════
    # PART A: Z → AGGREGATE SAVINGS COMPONENTS
    # ═══════════════════════════════════════════════════════════════════
    print("\n" + "=" * 70)
    print("PART A: Z → SAVINGS COMPONENTS (decomposition)")
    print("=" * 70)

    a_results = []

    # A1: Z → gross national savings
    r = run_model(df, 'gross_national_savings_gdp', demo_vars + controls_inc,
                  "A1: Z → nat'l savings")
    if r: a_results.append(r)

    # A2: Z → government saving
    r = run_model(df, 'govt_saving_gdp', demo_vars + controls_inc,
                  "A2: Z → govt saving")
    if r: a_results.append(r)

    # A3: Z → private saving
    r = run_model(df, 'private_saving_gdp', demo_vars + controls_inc,
                  "A3: Z → private saving")
    if r: a_results.append(r)

    # A4: Z → gross investment
    r = run_model(df, 'gross_investment_gdp', demo_vars + controls_inc,
                  "A4: Z → investment")
    if r: a_results.append(r)

    # A5: Z → S-I gap
    r = run_model(df, 'savings_investment_gap', demo_vars + controls_inc,
                  "A5: Z → S-I gap")
    if r: a_results.append(r)

    # A6: Z → CA (for comparison)
    r = run_model(df, 'ca_gdp', demo_vars + controls_inc,
                  "A6: Z → CA/GDP")
    if r: a_results.append(r)

    # A7: age ratios
    r = run_model(df, 'gross_national_savings_gdp', age_vars + controls_inc,
                  "A7: age ratios → savings")
    if r: a_results.append(r)

    r = run_model(df, 'govt_saving_gdp', age_vars + controls_inc,
                  "A8: age ratios → govt saving")
    if r: a_results.append(r)

    r = run_model(df, 'private_saving_gdp', age_vars + controls_inc,
                  "A9: age ratios → private saving")
    if r: a_results.append(r)

    key_a = demo_vars + age_vars
    build_table(a_results, key_a,
                f"Controls: {', '.join(controls_inc)}. Govt saving = revenue - expenditure. "
                f"Private saving = national savings - govt saving.",
                "decomposition.md",
                "Sectoral Savings Decomposition: Demographics → Savings Components")

    # ═══════════════════════════════════════════════════════════════════
    # PART B: OECD vs non-OECD
    # ═══════════════════════════════════════════════════════════════════
    print("\n" + "=" * 70)
    print("PART B: OECD vs non-OECD DECOMPOSITION")
    print("=" * 70)

    b_results = []

    for dep, label_prefix in [
        ('gross_national_savings_gdp', 'savings'),
        ('govt_saving_gdp', 'govt_saving'),
        ('private_saving_gdp', 'private_saving'),
    ]:
        r = run_model(oecd, dep, demo_vars + controls_inc,
                      f"OECD Z → {label_prefix}")
        if r: b_results.append(r)
        r = run_model(non_oecd, dep, demo_vars + controls_inc,
                      f"non-OECD Z → {label_prefix}")
        if r: b_results.append(r)

    build_table(b_results, demo_vars,
                "OECD vs non-OECD decomposition",
                "oecd_decomposition.md",
                "Sectoral Savings: OECD vs non-OECD")

    # ═══════════════════════════════════════════════════════════════════
    # PART C: INCOME TERCILES
    # ═══════════════════════════════════════════════════════════════════
    print("\n" + "=" * 70)
    print("PART C: INCOME TERCILES")
    print("=" * 70)

    c_results = []

    for dep in ['gross_national_savings_gdp', 'govt_saving_gdp', 'private_saving_gdp']:
        dep_short = dep.replace('_gdp', '').replace('gross_national_', '')
        for group in ['low', 'mid', 'high']:
            sub = df[df['income_group'] == group].copy()
            r = run_model(sub, dep, demo_vars + controls,
                          f"{group} Z → {dep_short}")
            if r: c_results.append(r)

    build_table(c_results, demo_vars,
                "Income tercile decomposition",
                "income_terciles.md",
                "Sectoral Savings by Income Tercile")

    # ═══════════════════════════════════════════════════════════════════
    # PART D: MEDIATION — Does govt saving mediate Z → CA?
    # ═══════════════════════════════════════════════════════════════════
    print("\n" + "=" * 70)
    print("PART D: MEDIATION — Does savings decomposition mediate Z → CA?")
    print("=" * 70)

    d_results = []

    # D1: Z → CA (baseline)
    r = run_model(df, 'ca_gdp', demo_vars + controls_inc, "D1: Z → CA (baseline)")
    if r: d_results.append(r)

    # D2: Z → CA + govt_saving
    if 'govt_saving_gdp' in df.columns:
        r = run_model(df, 'ca_gdp', demo_vars + controls_inc + ['govt_saving_gdp'],
                      "D2: Z → CA | govt_saving")
        if r: d_results.append(r)

    # D3: Z → CA + private_saving
    if 'private_saving_gdp' in df.columns:
        r = run_model(df, 'ca_gdp', demo_vars + controls_inc + ['private_saving_gdp'],
                      "D3: Z → CA | private_saving")
        if r: d_results.append(r)

    # D4: Z → CA + S-I gap
    if 'savings_investment_gap' in df.columns:
        r = run_model(df, 'ca_gdp', demo_vars + controls_inc + ['savings_investment_gap'],
                      "D4: Z → CA | S-I gap")
        if r: d_results.append(r)

    # Attenuation
    if len(d_results) >= 3:
        z1_base = d_results[0].get('coef_Z_1', None)
        for d in d_results[1:]:
            z1_med = d.get('coef_Z_1', None)
            if z1_base and z1_med and z1_base != 0:
                att = (1 - z1_med / z1_base) * 100
                print(f"  ★ {d['label']}: Z₁ attenuation = {att:.1f}%")

    key_d = demo_vars + ['govt_saving_gdp', 'private_saving_gdp', 'savings_investment_gap']
    build_table(d_results, key_d,
                "Sequential addition of savings components to test mediation of Z → CA",
                "mediation.md",
                "Mediation: Does Savings Composition Explain Z → CA?")

    # ═══════════════════════════════════════════════════════════════════
    # PART E: LAGGED & ROBUSTNESS
    # ═══════════════════════════════════════════════════════════════════
    print("\n" + "=" * 70)
    print("PART E: LAGGED & ROBUSTNESS")
    print("=" * 70)

    e_results = []

    lag_vars = ['Z_1_lag5', 'Z_2_lag5', 'Z_3_lag5']
    for dep in ['gross_national_savings_gdp', 'govt_saving_gdp', 'private_saving_gdp']:
        dep_short = dep.replace('_gdp', '').replace('gross_national_', '')
        r = run_model(df, dep, lag_vars + controls_inc, f"lag5 Z → {dep_short}")
        if r: e_results.append(r)

    build_table(e_results, lag_vars,
                "5-year lagged demographics",
                "robustness.md",
                "Robustness: Lagged Demographics → Savings Components")

    print("\n" + "=" * 70)
    print("Phase 2 complete.")
    print("=" * 70)


if __name__ == "__main__":
    main()
